package com.Tokiu.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/rep1")
public class ServletDemo4 extends HttpServlet {

    /*
       Servlet解决中文乱码问题
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.POST改变中文乱码
        //request.setCharacterEncoding("UTF-8");

        //2.GET改变中文乱码

        //获取name
        String username = request.getParameter("username");
        System.out.println("编码前："+username);

        byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);

        username=new String(bytes,StandardCharsets.UTF_8);

        System.out.println("编码后："+username);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
